package taller2.view.components;

import java.awt.Font;
import java.awt.GridBagLayout;

import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JToggleButton;

import taller2.controller.listeners.LoadCodeMenuListener;
import taller2.utils.Point;
import taller2.utils.Utils;
import taller2.view.menus.CodeBrowser;

public class LoadBar extends JPanel{
	private static final long serialVersionUID = 1L;
	private JTextField loadPath;
    private JToggleButton selectButton;
    private JToggleButton loadButton;

    public LoadBar(String codePath,CodeBrowser browser,int fontSize){
    	LoadCodeMenuListener handler = new LoadCodeMenuListener(this, browser);
    	
        selectButton = createButton("select",handler);
        loadButton = createButton("load",handler);
        loadPath = createTextField(codePath,fontSize,true);
        
        setLayout(new GridBagLayout());
        Utils.addComponent(this, loadPath, 1, 0, new Point(0, 0));
        Utils.addComponent(this, loadButton, 0, 0, new Point(1, 0));
        Utils.addComponent(this, selectButton, 0, 0, new Point(2, 0));

    }
    
    // unselects all buttons
    public void unselectButtons(){
            selectButton.setSelected(false);
            loadButton.setSelected(false);
    } 
    
    // changes the load path for the code
    public void setPath(String newPath){
            loadPath.setText(newPath);
    }
    
    // returns the load path for the code
    public String getPath(){
            return loadPath.getText();
    }
    
    // returns true if the object is the select button (for handler only!!!)
    public boolean isSelectButton(Object button){
            return (button == selectButton);
    }
   
    // returns true if the object is the load button (for handler only!!!)
    public boolean isLoadButton(Object button){
            return (button == loadButton);
    }
   
    // adds a standard button
    private JToggleButton createButton(String text,LoadCodeMenuListener handler){
            JToggleButton button = new JToggleButton(text);
            button.addActionListener(handler);              
            return button;
    }      
   
    // adds a text field from:
    // its default text , its size , whether is editable or not
    private JTextField createTextField(String text, int fontSize ,boolean editable){
            JTextField field = new JTextField(text);
            field.setEditable(editable);
            field.setFont(new Font("Serif",Font.PLAIN,fontSize));
            return field;
    }   
}
